/**
 * The MIT License (MIT)
 * Copyright (c) 2020 铭软科技(mingsoft.net)
 * Permission is hereby granted, free of charge, to any person obtaining a copy of
 * this software and associated documentation files (the "Software"), to deal in
 * the Software without restriction, including without limitation the rights to
 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
 * the Software, and to permit persons to whom the Software is furnished to do so,
 * subject to the following conditions:
 * <p>
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 * <p>
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
 * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
 * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
 * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */
package net.mingsoft;

/*import net.mingsoft.kmessage.DebugForKafkaMessage;
import net.mingsoft.kmessage.consumer.GELIConsumer;
import net.mingsoft.kmessage.consumer.GeliKafkaHandler;*/
import net.mingsoft.basic.util.ApplicationContextUtil;
import net.mingsoft.report_data.sao.SgSao;
import org.mybatis.spring.annotation.MapperScan;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.ServletComponentScan;
import org.springframework.context.ApplicationContext;

import org.springframework.context.annotation.Bean;
import org.springframework.scheduling.TaskScheduler;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import javax.annotation.Resource;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.LockSupport;


@SpringBootApplication(scanBasePackages = {"net.mingsoft"}, exclude = {
        com.alibaba.druid.spring.boot.autoconfigure.DruidDataSourceAutoConfigure.class})
@MapperScan(basePackages = {"**.dao", "com.baomidou.**.mapper","net.mingsoft.flk.mapper"})
@ServletComponentScan(basePackages = {"net.mingsoft"})
@EnableScheduling
public class MSApplication implements ApplicationRunner {


    public static void main(String[] args) {

        MSApplication.applicationContext = SpringApplication.run(MSApplication.class, args);
        applicationDone = true;

        // 配置上下文工具类
        ApplicationContextUtil.setApplicationContext(MSApplication.applicationContext);

        LockSupport.parkNanos(MSApplication.class, TimeUnit.SECONDS.toNanos(3));
        // 启动隔离外网的 Kafka 消费处理器。
        //startGeliKafkaListener();
    }

    @Bean
    public TaskScheduler taskScheduler() {
        ThreadPoolTaskScheduler threadPoolTaskScheduler = new ThreadPoolTaskScheduler();
        threadPoolTaskScheduler.setPoolSize(3);
        return threadPoolTaskScheduler;
    }

    /**
     * 获取上下文，是用spring 容器管理bean
     */
    public static ApplicationContext applicationContext;
    public static boolean applicationDone;
    private static final Logger logger = LoggerFactory.getLogger(MSApplication.class);

    @Resource
    SgSao sgSao;

    @Override
    public void run(ApplicationArguments args) throws Exception {
        // initialize sg data
//        sgSao.login();
    }

    /**
     * 启动外网消费程序
     */
    /*private static void startGeliKafkaListener() {
        // 判断是否为发布版本，开发版本不启动监听
        logger.info("》》》启动 Geli-Kafka 消费处理器 》》》");
        final GELIConsumer consumer = applicationContext.getBean(GELIConsumer.class);
        final GeliKafkaHandler geliKafkaHandler = applicationContext.getBean(GeliKafkaHandler.class);
        geliKafkaHandler.initialize(consumer, null);
        logger.info("》》》启动 Geli-Kafka 消费处理器【成功！】》》》");
    } // startGeliKafkaListener()*/
}
